package com.mobileClientKernel.android.serviceManager.network;

import com.mobileClientKernel.android.serviceManager.help.JJMD5;
import com.mobileClientKernel.android.serviceManager.service.IJJServiceCallBack;
import com.mobileClientKernel.android.commonLayer.JJEncrypt;

import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by hamilyjing on 4/11/16.
 */
public class JJRequest extends JJBaseRequest {
    private String identity;

    private Class modelClass;

    private boolean isSaveToMemory;
    private boolean isSaveToDisk;

    private String userCacheDirectory;

    private String sensitiveDataForSavedFileName = "";
    private JSONObject parametersForSavedFileName;

    private Object cacheModel;
    private Object oldModel;

    private IResponseOperation responseOperation;

    private IJJServiceCallBack serviceCallBack;

    private boolean isNeedResponseNotification = false;

    @Override
    public void requestCompleteFilter() {
        super.requestCompleteFilter();

        setOldModel(getCacheModel());

        Object model = currentResponseModel();

        if (!successForBussiness(model)) {
            return;
        }

        if (isSaveToMemory) {
            setCacheModel(model);
        }

        if (isSaveToDisk) {
            saveObjectToDisk(model);
        }
    }

    /// operation

    public String filterResponseString(String responseString) {
        return responseString;
    }

    public Object currentResponseModel() {
        String filterString = filterResponseString(this.getResponseString());
        Object model = convertToModel(filterString);
        model = operateWithNewObject(model, getOldModel());
        return model;
    }

    public boolean successForBussiness(Object model) {
        return false;
    }

    public Object convertToModel(String responseString) {
        return null;
    }

    public Object operateWithNewObject(Object newModel, Object oldModel) {
        if (responseOperation != null) {
            return responseOperation.operate(newModel, oldModel);
        }

        return newModel;
    }

    /// cache

    public Object getCacheFromDisk() {
        Object model = JJEncrypt.readObjfromFile(savedFilePath());
        return model;
    }

    public void saveObjectToMemory(Object model) {
        setCacheModel(model);
    }

    public void saveObjectToDisk(Object model) {
        JJEncrypt.writeObj2File(model, savedFilePath());
    }

    public boolean haveDiskCache() {
        File file = new File(savedFilePath());
        if (file.isFile() && file.exists()) {
            return true;
        }
        return false;
    }

    public void removeMemoryCache() {
        setCacheModel(null);
    }

    public void removeDiskCache() {
        File file = new File(savedFilePath());
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    }

    public void removeAllCache() {
        removeMemoryCache();
        removeDiskCache();
    }

    /// disk file

    public String savedFilePath() {
        String filePath = savedFileDirectory() + "/" + savedFileName();
        return filePath;
    }

    public String savedFileDirectory() {
        String directory = getContext().getFilesDir().getPath();

        directory = directory + "/JJServiceModelCache";

        if (getUserCacheDirectory() != null && getUserCacheDirectory().length() > 0) {
            directory = directory + "/" + getUserCacheDirectory();
        }

        File destDir = new File(directory);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        return directory;
    }

    public String savedFileName() {
        String cacheFileName = "";

        //
        String sensitiveData = (sensitiveDataForSavedFileName != null) ? sensitiveDataForSavedFileName : "";
        cacheFileName += sensitiveData + "_";

        //
        cacheFileName += (identity != null) ? identity : "" + "_";

        //
        String argumentSting = "";
        JSONObject argument = parametersForSavedFileName;
        if (argument != null) {
            argumentSting = argument.toString();
        }
        argumentSting = JJMD5.md5(argumentSting);
        cacheFileName += argumentSting;

        return cacheFileName;
    }

    /// get and set


    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }

    public boolean getIsSaveToMemory() {
        return isSaveToMemory;
    }

    public void setIsSaveToMemory(boolean isSaveToMemory) {
        this.isSaveToMemory = isSaveToMemory;
    }

    public boolean getIsSaveToDisk() {
        return isSaveToDisk;
    }

    public void setIsSaveToDisk(boolean isSaveToDisk) {
        this.isSaveToDisk = isSaveToDisk;
    }

    public Object getOldModel() {
        return oldModel;
    }

    public void setOldModel(Object oldModel) {
        this.oldModel = oldModel;
    }

    public Class getModelClass() {
        return modelClass;
    }

    public void setModelClass(Class modelClass) {
        this.modelClass = modelClass;
    }

    public Object getCacheModel() {
        if (this.cacheModel != null) {
            return this.cacheModel;
        }

        Object model = null;
        if (getIsSaveToDisk()) {
            model = getCacheFromDisk();
            if (getIsSaveToMemory()) {
                this.cacheModel = model;
            }
        }

        return model;
    }

    public void setCacheModel(Object cacheModel) {
        this.cacheModel = cacheModel;
    }

    public IResponseOperation getResponseOperation() {
        return responseOperation;
    }

    public void setResponseOperation(IResponseOperation responseOperation) {
        this.responseOperation = responseOperation;
    }

    public String getUserCacheDirectory() {
        return userCacheDirectory;
    }

    public void setUserCacheDirectory(String userCacheDirectory) {
        this.userCacheDirectory = userCacheDirectory;
    }

    public String getSensitiveDataForSavedFileName() {
        return sensitiveDataForSavedFileName;
    }

    public void setSensitiveDataForSavedFileName(String sensitiveDataForSavedFileName) {
        this.sensitiveDataForSavedFileName = sensitiveDataForSavedFileName;
    }

    public IJJServiceCallBack getServiceCallBack() {
        return serviceCallBack;
    }

    public void setServiceCallBack(IJJServiceCallBack serviceCallBack) {
        this.serviceCallBack = serviceCallBack;
    }

    public void setParametersForSavedFileName(JSONObject parametersForSavedFileName) {
        this.parametersForSavedFileName = parametersForSavedFileName;
    }

    public boolean getIsNeedResponseNotification() {
        return isNeedResponseNotification;
    }

    public void setIsNeedResponseNotification(boolean isNeedResponseNotification) {
        this.isNeedResponseNotification = isNeedResponseNotification;
    }
}
